package xyz.lament.springehcache;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/springehcache")
public class UseCacheController
{
	@Autowired
	UseCacheService service;
	
	@ResponseBody
	@RequestMapping(value="/user/{uid}",method=RequestMethod.GET)	
    public UserBean getUser(@PathVariable("uid") String uid)
	{
		UserBean serviceUser = service.getUser(uid);
		if(serviceUser != null)
		{
			System.out.println("get: " + serviceUser);
		}
		else
		{
			System.out.println("can't get: " + uid);
		}
        return serviceUser;
    }
	
	@ResponseBody
	@RequestMapping(value="/user",method=RequestMethod.POST)	
    public void putUser(@RequestBody UserBean user)
	{		
		if(user != null)
		{
			service.putUser(user);
			System.out.println("put: " + user);
		}
		else
		{
			System.out.println("user is null.");
		}
    }
	
	@ResponseBody
	@RequestMapping(value="/user/{uid}",method=RequestMethod.DELETE)	
    public void deleteUser(@PathVariable("uid") String uid)
	{		
		if(StringUtils.isEmpty(uid))
		{
			System.out.println("the uid is empty,can't delete.");
		}
		else
		{
			service.deleteUser(uid);
			System.out.println("user is deleted. uid: " + uid);
		}
    }
}
